package com.zuoqi.easywork.projectManage.controller;

import com.github.pagehelper.Page;
import com.zuoqi.common.utils.IMap;
import com.zuoqi.common.utils.PageView;
import com.zuoqi.common.utils.ReturnMapUtils;
import com.zuoqi.common.utils.SessionUtil;
import com.zuoqi.easywork.moneyManage.service.FundService;
import com.zuoqi.easywork.projectManage.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
public class projectController {
    @Autowired
    private ProjectService projectService;

    @RequestMapping(value = "/projectCon/toPage/projectManageView")
    public String projectManageView(){
        return "/module/projectManage/projectManage";
    }
    /**
     * 获得项目列表
     * */
    @RequestMapping(value = "/projectCon/ajax/getProjectList",method = RequestMethod.POST)
    @ResponseBody
    public Map getProjectList(HttpServletRequest request){
        IMap params = new IMap(request);
        Page page = PageView.startPageForTable(params, false);
        List<IMap> projectList = projectService.getProjectListForManage(params);
        Map<String, Object> result = PageView.getResultForTable(page, projectList);
        return result;
    }
    /**
     * 添加新项目
     * */
    @RequestMapping(value = "/projectCon/toPage/addProjectView")
    public String addProjectView(){
        return "/module/projectManage/addProject";
    }

    @RequestMapping(value = "/projectCon/ajax/addProject",method = RequestMethod.POST)
    @ResponseBody
    public Map addProject(HttpServletRequest request){
        IMap params = new IMap(request);
        HttpSession session = request.getSession();
        String userId = SessionUtil.getLoginUserId(session);
        params.put("updateUser",userId);
        int i = projectService.addProject(params);
        if (i==1){
            return ReturnMapUtils.businessResult("0000","添加成功！（未启动前可修改）");
        }else {
            return ReturnMapUtils.businessResult("9999","服务器错误，添加失败！");
        }
    }
    /**
     * 项目修改
     * */
    @RequestMapping(value = "/projectCon/toPage/updProjectView")
    public String updProjectView(HttpServletRequest request,ModelMap modelMap){
        IMap params = new IMap(request);
        IMap thisProject = projectService.getProjactInfo(params.getString("proId"));
        modelMap.addAttribute("thisProject",thisProject);
        return "/module/projectManage/updProjectInfo";
    }
    @RequestMapping(value = "/projectCon/ajax/UpdProject",method = RequestMethod.POST)
    @ResponseBody
    public Map UpdProject(HttpServletRequest request){
        IMap params = new IMap(request);
        HttpSession session = request.getSession();
        String userId = SessionUtil.getLoginUserId(session);
        params.put("updateUser",userId);
        int i = projectService.UpdProject(params);
        if (i==1){
            return ReturnMapUtils.businessResult("0000","修改成功！");
        }else {
            return ReturnMapUtils.businessResult("9999","服务器错误，修改失败！");
        }
    }
    /**
     * 确定启动
     */
    @RequestMapping(value = "/projectCon/ajax/doStart",method = RequestMethod.POST)
    @ResponseBody
    public Map doStart(HttpServletRequest request){
        IMap params = new IMap(request);
        HttpSession session = request.getSession();
        String userId = SessionUtil.getLoginUserId(session);
        params.put("updateUser",userId);
        int i = projectService.doStart(params);
        if (i==1){
            return ReturnMapUtils.businessResult("0000","启动成功！");
        }else {
            return ReturnMapUtils.businessResult("9999","服务器错误，启动失败！");
        }
    }
    /**
     * 确定作废
     */
    @RequestMapping(value = "/projectCon/ajax/doEnd",method = RequestMethod.POST)
    @ResponseBody
    public Map doEnd(HttpServletRequest request){
        IMap params = new IMap(request);
        HttpSession session = request.getSession();
        String userId = SessionUtil.getLoginUserId(session);
        params.put("updateUser",userId);
        int i = projectService.doEnd(params);
        if (i==1){
            return ReturnMapUtils.businessResult("0000","作废成功！");
        }else {
            return ReturnMapUtils.businessResult("9999","服务器错误，作废失败！");
        }
    }
    @RequestMapping(value = "/projectCon/ajax/addProjectFund",method = RequestMethod.POST)
    @ResponseBody
    public Map addProjectFund(HttpServletRequest request){
        IMap params = new IMap(request);
        HttpSession session = request.getSession();
        String userId = SessionUtil.getLoginUserId(session);
        params.put("updateUser",userId);
        int check = projectService.fundCheck(params.getString("addMoney"));
        if (check == 0){
            return ReturnMapUtils.businessResult("9999","公司账户可用金额不足！");
        }
        int i = projectService.addProjectFund(params);
        if (i==1){
            return ReturnMapUtils.businessResult("0000","增加成功！");
        }else {
            return ReturnMapUtils.businessResult("9999","服务器错误，增加失败！");
        }
    }
    /**
     * 确定结束
     */
    @RequestMapping(value = "/projectCon/ajax/doEndProject",method = RequestMethod.POST)
    @ResponseBody
    public Map doEndProject(HttpServletRequest request){
        IMap params = new IMap(request);
        HttpSession session = request.getSession();
        String userId = SessionUtil.getLoginUserId(session);
        params.put("updateUser",userId);
        int i = projectService.doEndProject(params);
        if (i==1){
            return ReturnMapUtils.businessResult("0000","结束项目成功！");
        }else {
            return ReturnMapUtils.businessResult("9999","服务器错误，结束项目失败！");
        }
    }
}
